<?php

namespace App\Admin\Actions\Grid\Order;

use App\Enums\OrderPay;
use App\Models\Order;
use App\Services\OrderService;
use Dcat\Admin\Actions\Response;
use Dcat\Admin\Grid\RowAction;

class PaySuccess extends RowAction
{
    /**
     * @return string
     */
    protected $title = '设为已支付';

    /**
     * Handle the action request.
     *
     *
     * @return Response
     */
    public function handle(): Response
    {
        $order = Order::query()->where('id', $this->getKey())->first();
        if ($order->pay == OrderPay::WAIT) {
            app(OrderService::class)->paySuccess($order->order_no, 'test' . onlyOrderSn(), now()->toDateTimeString(), []);
        }
        return $this->response()
            ->success('操作成功')
            ->refresh();
    }

    /**
     * @return string
     */
    public function confirm(): string
    {
        return '确定将此订单设置为已支付吗？';
    }

    public function html(): string
    {
        $this->defaultHtmlAttribute('href', 'javascript:void(0)');

        return <<<HTML
<a {$this->formatHtmlAttributes()}><i class="feather icon-check-square"> {$this->title()}</i></a>
HTML;
    }

}
